************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Figure 8 (Nap Treatment Effects on Productivity throughout the Day)
* Last edited: 07 May 2021
************************************************

	set more off
	cap reghdfe, compile 
	
	******************************************
	* Set-up *********************************
	******************************************
			
	use "$d/typing_by_clock.dta", replace
	
	drop if day_in_study==.
	
	drop if day_in_study == 1

	gen clock_just_hours = hh(clock)
	gen clock_just_minutes = mm(clock)
	
	gen clock_min = clock_just_hours*60 + clock_just_minutes
	gen clock_hr = clock_just_hours + clock_just_minutes/60
	
	gen break_group = (post_lunch_activity==1)
	gen treat_nap = (post_lunch_activity==2)
	gen extra_work = (post_lunch_activity==3)
	
	gen treat_pool = (treatment_group==1 | treatment_group==2)
	
	drop _merge
	merge m:1 pid date using "$d/typing_merged_pid_day.dta", keepusing(day_type fraction_high checkin)
	
	drop _merge
	merge m:1 pid date using "$d/actigraph_night_cleaned.dta",
	
	gen long_day = (day_type==0)
	
	gen clock_30min = 570 + 30*ceil((clock_min - 569)/30) ///Diving by 569 makes bins [9:30, 10) and so on
	
	gen morning = (clock_hr <= 13.5)

	collapse (mean) treat_nap treat_pool extra_work break_group female age fraction_high long_day day_type checkin post_lunch_activity (sum) output typing_time , by(pid day_in_study date clock_30min post_treatment) fast
	
	drop if typing_time==0
	
	gen productivity = output/(typing_time/60)

	
	gen clock_sleep = clock_30min * treat_pool
	gen clock_nap = clock_30min * treat_nap
	gen clock_break = clock_30min * break_group
	gen clock_extra = clock_30min * extra_work
	gen clock_activity = .
	
***********************************************
* NAP - Main graph
***********************************************

preserve
	
cap drop coef coef_ub coef_lb x
	
	gen coef = .
	gen coef_ub = .
	gen coef_lb = .
	gen x = .
	
	forval i = 1/18 {
	replace x = `i' in `i'
	}
	
	
eststo clear
	
	replace clock_activity = clock_nap 
	
	cap drop _baseline baseline
	gen _baseline = productivity if post_treatment==0 
	bys pid: egen baseline = mean(_baseline)
	
	eststo napcontrol: reghdfe productivity i.clock_activity if post_treatment==1 & clock_30min<=1170 & clock_30min>=630 & clock_30min!=840 & day_in_study!=28, ///
	cluster(pid) absorb(date#clock_30min day_in_study#clock_30min female#clock_30min c.age#clock_30min c.baseline#clock_30min c.fraction_high#clock_30min i.day_type#clock_30min) coeflegend
	
	forval i = 2/18 {
	if (`i'!= 7 & `i'!=9) { //Coefficients except lunch and nap time
	local j = 570 + `i'*30
	lincom _b[`j'.clock_activity]
	
	replace coef = r(estimate) if x==`i'
	replace coef_ub = r(estimate) + 1.645*r(se) if x==`i'
	replace coef_lb = r(estimate) - 1.645*r(se) if x==`i'
	}
	}

collapse coef*, by(x)
				
			*Coordinates for LUNCH arrow
			gen arrowx1 = 5.7
			gen arrowx2 = 6.5
			gen arrowy1 = 195
			gen arrowy2 = 195
						
			*Coordinates for NAP arrow
			gen arrow2x1 = 10.3
			gen arrow2x2 = 9.5
			gen arrow2y1 = 195
			gen arrow2y2 = 195
*
	twoway ///
			(scatter coef x if x!=7 & x!=9, color("238 118 0") lcolor(black) mcolor("62 97 128") msymb(diamond) sort) ///
			(rspike coef_ub coef_lb x if x!=7 & x!=9, lcolor(black) lcolor("62 97 128") lwidth(vvthin)) ///
			(pcarrow arrowy1 arrowx1 arrowy2 arrowx2, mcolor(black) lcolor(black) msize(medsmall)) ///
			(pcarrow arrow2y1 arrow2x1 arrow2y2 arrow2x2, mcolor(black) lcolor(black) msize(medsmall)), ///
			xline(9, lwidth(4) lc(gs14)) ///
			xline(7, lwidth(4) lc(gs14)) ///
			xscale(r(2 19)) xlabel(2 "10:00-10:30" 4 "11:00-11:30" 6 "12:00-12:30" 8 "13:00-13:30" 10 "14:00-14:30" ///
			12 "15:00-15:30" 14 "16:00-16:30" 16 "17:00-17:30" 18 "18:00-18:30", angle(45)) ///
			xtitle("", height(6)) ytitle("Productivity", height(6)) title("Treatment period") /// X-AXIS
			yscale(r(-400 600)) ylabel(-200(200)400) yline(0, lcolor("100 100 100")) ///
			legend(order(1 "Nap") cols(1) region(lcolor(white)) symysize(1.5) symxsize(2) size(3.5)) aspectratio(0.55) ///
			graphregion(fcolor(white) margin(0 0 0 0)) plotregion(margin(0 0 0 0)) ///
			text(195 5 "{it:Lunch}", place(c) size(small) color(black)) ///
			text(195 10.7 "{it:Nap}", place(c) size(small) color(black))

			
			graph export "$oa/Figures/FigureA8_productivity_by_clock_nap.eps", as(eps) replace
			graph export "$oa/Figures/FigureA8_productivity_by_clock_nap.png", as(png) replace

restore
	
